package files.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;

@Configuration  //配置类
public class FilesConfig {
    //阿里云OSS文件上传功能初始化
    //获取阿里云OSS的endpoint
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //获取阿里云OSS的accessKeyId
    @Value("${aliyun.oss.access-key-id}")
    private String accessKeyId;

    //获取阿里云OSS的accessKeySecret
    @Value("${aliyun.oss.access-key-secret}")
    private String accessKeySecret;
    
    //获取阿里云OSS的bucketName
    @Value("${aliyun.oss.bucket-name}")
    private String bucketName;
    
    //获取阿里云OSS的基础目录
    @Value("${aliyun.oss.base-dir}")
    private String baseDir;
    
    //获取阿里云OSS的样式规则
    @Value("${aliyun.oss.style-rules.thumbnail}")
    private String thumbnailUrl;

    @Bean
    public OSS ossClient() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
    
    @Bean
    public String bucketName() {
        return bucketName;
    }
    
    @Bean
    public String baseDir() {
        return baseDir;
    }
    
    @Bean
    public String baseUrl() {
        return thumbnailUrl.substring(0, thumbnailUrl.indexOf(".com") + 4);
    }
}